package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.bean.XiangmuBean2;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SelectXiangmu extends BaseActivity implements View.OnClickListener {

    private RecyclerView recycler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_xiangmu);
        initTitle();
    }

    private void initTitle() {
        TextView title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("选择项目");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        recycler = (RecyclerView) findViewById(R.id.recycler);
        recycler.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

    }

    private List<XiangmuBean2.InfoEntity> info1 = new ArrayList<>();

    private void updown() {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        showDialog();
        XUtil.Post(Url.DECORATE_BUDGETLIST, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        XiangmuBean2 bean2 = gson.fromJson(result, XiangmuBean2.class);
                        info1 = bean2.getInfo();
                        setDate();
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();

                super.onFinished();
            }


            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
            }
        });


    }

    private void setDate() {
        recycler.setAdapter(new BaseRVAdapter(this, info1) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.all_project_item2;
            }

            @Override
            public void onBind(BaseViewHolder viewHolder, final int position) {
                viewHolder.getTextView(R.id.order_time).setText("下单时间：" + info1.get(position).getOrder_time());
                viewHolder.getTextView(R.id.tv_type).setTextColor(0xffe9573d);
                viewHolder.getView(R.id.rl_type1).setVisibility(View.GONE);
                viewHolder.getView(R.id.rl_type2).setVisibility(View.GONE);

                String str = "项目名称:<font color=\"#e9573d\">" + info1.get(position).getRecord_name() + "</font>";
                viewHolder.getTextView(R.id.xiangmu_name).setText(Html.fromHtml(str));

                viewHolder.getTextView(R.id.tv_project_code).setText("合同号:" + info1.get(position).getBudget_no());

                viewHolder.getTextView(R.id.tv_goods_number).setText("共" + info1.get(position).getCount() + "套商品 合计：¥");
                viewHolder.getTextView(R.id.tv_all_price).setText(info1.get(position).getTotal_fee());
                RecyclerView recyclerView = viewHolder.getView(R.id.recycler);
                recyclerView.setLayoutManager(new LinearLayoutManager(SelectXiangmu.this, LinearLayoutManager.VERTICAL, false));
                final List<XiangmuBean2.InfoEntity.DataEntity> dataList = info1.get(position).getData();
                recyclerView.setAdapter(new BaseRVAdapter(SelectXiangmu.this, dataList) {
                    @Override
                    public int getLayoutId(int viewType) {
                        return R.layout.project_content_item;
                    }

                    @Override
                    public void onBind(BaseViewHolder viewHolder, int position2) {
                        x.image().bind(viewHolder.getImageView(R.id.iv_img), dataList.get(position2).getGoods_pic());
                        viewHolder.getTextView(R.id.tv_goods_name).setText(dataList.get(position2).getGoods_name());
//                        if (!"整体浴室".equals(dataList.get(position2).getPackage_cate())) {
//                            viewHolder.getTextView(R.id.tv_product).setBackground(getResources().getDrawable(R.drawable.button_ju));
//                        } else {
//                            viewHolder.getTextView(R.id.tv_product).setBackground(getResources().getDrawable(R.drawable.button_main));
//                        }
                        if ("1".equals(dataList.get(position2).getTypeid())) {
                            viewHolder.getTextView(R.id.tv_product).setBackground(getResources().getDrawable(R.drawable.button_ju));
                        } else if ("0".equals(dataList.get(position2).getTypeid())) {
                            viewHolder.getTextView(R.id.tv_product).setBackground(getResources().getDrawable(R.drawable.button_main));
                        } else if ("2".equals(dataList.get(position2).getTypeid())) {
                            viewHolder.getTextView(R.id.tv_product).setBackground(getResources().getDrawable(R.drawable.buttom_green));
                        }
                        viewHolder.getTextView(R.id.tv_product).setText(dataList.get(position2).getPackage_cate());
                        viewHolder.getTextView(R.id.tv_goods_info).setText(dataList.get(position2).getSku_info());
                        viewHolder.getTextView(R.id.tv_goods_type).setText("房间号:" + dataList.get(position2).getRoom_number());
                        if ("1".equals(dataList.get(position2).getIf_tepi())) {
                            String a = "<s>" + "¥ " + dataList.get(position2).getPrice() + "</s>";
                            viewHolder.getTextView(R.id.tv_price2).setText(Html.fromHtml(a));
                            viewHolder.getTextView(R.id.tv_price2).setVisibility(View.VISIBLE);
                            viewHolder.getTextView(R.id.tv_price2).getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                            viewHolder.getTextView(R.id.tv_price).setText("¥ " + dataList.get(position2).getTepi_price());
                        } else {
                            viewHolder.getTextView(R.id.tv_price).setText("¥ " + dataList.get(position2).getPrice());
                        }

                        viewHolder.getTextView(R.id.tv_price2).setText("¥ " + dataList.get(position2).getPrice());
                        viewHolder.getTextView(R.id.tv_goods_number).setText("x" + dataList.get(position2).getNums());
                        viewHolder.getView(R.id.ll_max_item).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = getIntent();
                                intent.putExtra("name", info1.get(position).getRecord_name());
                                intent.putExtra("id", info1.get(position).getBudget_id());
                                setResult(RESULT_OK, intent);
                                finish();
                            }
                        });
                    }

                });
                viewHolder.getView(R.id.ll_max).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = getIntent();
                        intent.putExtra("name", info1.get(position).getRecord_name());
                        intent.putExtra("id", info1.get(position).getBudget_id());
                        setResult(RESULT_OK, intent);
                        finish();
                    }
                });
            }

        });
    }


    @Override
    protected void onResume() {
        updown();
        super.onResume();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
        }
    }
}
